from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, ExecuteProcess
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node
from launch_ros.substitutions import FindPackagePrefix
from launch.substitutions import PathJoinSubstitution
import os, os.path as osp


def generate_launch_description():
    bag_name = LaunchConfiguration('bag_name')

    bag_name_arg = DeclareLaunchArgument(
        'bag_name',
        default_value='rosbag_10'
    )

    bag_player = ExecuteProcess(
        cmd=[
            'ros2',
            'bag',
            'play',
            '--loop',
            PathJoinSubstitution([
                FindPackagePrefix('3dsot_ros'),
                'bags',
                bag_name
            ])
        ],
        shell=False
    )

    return LaunchDescription(
        [
            bag_name_arg,
            bag_player,
            Node(
                package='3dsot_ros',
                executable='tracker',
                name='tracker',
                output='screen',
                # prefix=['stdbuf -o L'],
                emulate_tty=True
            )
        ]
    )